﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using E2_WEBSITE.Code.Product;
using E2_WEBSITE.Code;
using System.Drawing;

namespace E2_WEBSITE.Products
{
    public partial class detail : System.Web.UI.Page
    {
        
        private ProductController m_ProductController = new ProductController();

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                BindingTable();
            }
            else
            {
                ImageButton imgBtn;
                imgBtn = this.Master.FindControl("ContentPlaceHolder1").FindControl("btnAddFavorite") as ImageButton;
                if (imgBtn != null)
                {
                    imgBtn.Click += new ImageClickEventHandler(imgBtn_Click);                    
                }
                
            }
        }

        private void runjQueryCode(string jsCodetoRun)
        {

            ScriptManager requestSM = ScriptManager.GetCurrent(this);
            if (requestSM != null && requestSM.IsInAsyncPostBack)
            {
                ScriptManager.RegisterClientScriptBlock(this,
                                                        typeof(Page),
                                                        Guid.NewGuid().ToString(),
                                                        jsCodetoRun,
                                                        true);
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(typeof(Page),
                                                       Guid.NewGuid().ToString(),
                                                       jsCodetoRun, true);
            }
        }

        void imgBtn_Click(object sender, ImageClickEventArgs e)
        {
            int iDaDangNhap = (Int32)Session["IsLogin"];
            if (iDaDangNhap != 1)
            {
                runjQueryCode("alert('Bạn phải đăng nhập trước')");

            }
            else
            {                
                string sProductId = Request.QueryString["ProductId"];
                int userId = (Int32)Session["UserID"];
                m_ProductController.AddFavoriteProduct(userId, int.Parse(sProductId));
            }
        }

        private void BindingTable()
        {
            string sProductId = string.Empty;

            sProductId = Request.QueryString["ProductId"];

            if (sProductId != null)
            {
                ImageButton imgBtn;
                imgBtn = this.Master.FindControl("ContentPlaceHolder1").FindControl("btnCompare") as ImageButton;
                if (imgBtn != null)
                {
                    imgBtn.PostBackUrl = "compare.aspx?ProId=" + sProductId;
                }
                                
                //view basic information
                DataTable dt = null;
                ProductInfo product = null;

                product = m_ProductController.getProductDetail(sProductId, "", out dt);

                if (dt == null || product == null)
                {
                    return;
                }
                
                imgBtn = this.Master.FindControl("ContentPlaceHolder1").FindControl("btnAddToCart") as ImageButton;
                if (imgBtn != null)
                {
                    imgBtn.PostBackUrl = "~/cp/member/addtocart.aspx?pID=" + sProductId + "&cID=" + product.Hinh_Anh.ID + "&quan=1";
                }

                imgBtn = this.Master.FindControl("ContentPlaceHolder1").FindControl("btnCustomize") as ImageButton;
                if (imgBtn != null)
                {
                    imgBtn.PostBackUrl = "customize.aspx?ProId=" + sProductId;
                }

                string sCategory = product.Loai_San_Pham.TenLoaiSanPham.ToLower();
                if (sCategory.CompareTo("laptop") == 0)
                {
                    imgBtn.Enabled = false;
                }

                lNameProduct.Text = product.TenSanPham;
                lProductCost.Text = Utilities.ConvertToMoneyFormat(product.Gia) + " VNĐ";
                imgProduct.ImageUrl = product.Hinh_Anh.URL;

                //specific information
                Table table = this.Master.FindControl("ContentPlaceHolder1").FindControl("TableDetailProduct") as Table;
                if (dt.Rows.Count == 0)
                {
                    InsertRow(table, "", "Không tìm thấy dữ liệu!");
                    return;
                }
                switch (product.Loai_San_Pham.TenLoaiSanPham)
                {
                    case "CPU":

                        InsertRow(table, "Socket", dt.Rows[0]["Socket"].ToString());
                        InsertRow(table, "Bus", dt.Rows[0]["Bus"].ToString());
                        InsertRow(table, " Cache", dt.Rows[0]["Cache"].ToString());
                        InsertRow(table, "Speed", dt.Rows[0]["Speed"].ToString());

                        break;

                    case "RAM":

                        InsertRow(table, "Bus", dt.Rows[0]["Bus"].ToString());
                        InsertRow(table, "Dung Luong", dt.Rows[0]["DungLuong"].ToString());
                        break;
                    case "SOUNDCARD":

                        InsertRow(table, "Cong Suat", dt.Rows[0]["CongSuat"].ToString());
                        InsertRow(table, "Ex", dt.Rows[0]["Ex"].ToString());

                        break;
                    case "MONITOR":

                        InsertRow(table, "Do Phan Giai", dt.Rows[0]["DoPhanGiai"].ToString());
                        InsertRow(table, "Do Tuong Phan", dt.Rows[0]["DoTuongPhan"].ToString());
                        InsertRow(table, "Thoi Gian Dap Ung", dt.Rows[0]["ThoiGianDapUng"].ToString());
                        InsertRow(table, "Cong Giao Tiep", dt.Rows[0]["CongGiaoTiep"].ToString());

                        break;
                    case "CASE":

                        InsertRow(table, "Kich Thuoc", dt.Rows[0]["KichThuoc"].ToString());
                        InsertRow(table, "Vat Lieu", dt.Rows[0]["VatLieu"].ToString());
                        InsertRow(table, "Ho Tro", dt.Rows[0]["HoTro"].ToString());
                        InsertRow(table, "Nguon", dt.Rows[0]["Nguon"].ToString());
                        InsertRow(table, "Cong Giao Tiep", dt.Rows[0]["CongGiaoTiep"].ToString());
                        InsertRow(table, "Khong Gian Chua", dt.Rows[0]["KhongGianChua"].ToString());
                        InsertRow(table, "Mo Rong", dt.Rows[0]["MoRong"].ToString());
                        InsertRow(table, "Tan Nhiet", dt.Rows[0]["TanNhiet"].ToString());

                        break;

                    case "KEYBOARD":

                        InsertRow(table, "Cong Giao Tiep", dt.Rows[0]["CongGiaoTiep"].ToString());
                        InsertRow(table, "Mau", dt.Rows[0]["Mau"].ToString());

                        break;

                    case "SPEAKER":

                        InsertRow(table, "Cong Suat", dt.Rows[0]["CongSuat"].ToString());
                        InsertRow(table, "Chuan", dt.Rows[0]["Chuan"].ToString());

                        break;
                    case "VGA":

                        InsertRow(table, "Engine", dt.Rows[0]["Engine"].ToString());
                        InsertRow(table, "Video Memory", dt.Rows[0]["VideoMemory"].ToString());
                        InsertRow(table, "Interface Memory", dt.Rows[0]["InterfaceMemory"].ToString());
                        InsertRow(table, "Cong Giao Tiep", dt.Rows[0]["CongGiaoTiep"].ToString());

                        break;
                    case "POWER":

                        InsertRow(table, "Cong Suat", dt.Rows[0]["CongSuat"].ToString());
                        InsertRow(table, "Quat", dt.Rows[0]["Quat"].ToString());
                        InsertRow(table, "Connector", dt.Rows[0]["Connector"].ToString());

                        break;
                    case "CD_DVD":

                        InsertRow(table, "Toc Do", dt.Rows[0]["TocDo"].ToString());
                        InsertRow(table, "Cong Giao Tiep", dt.Rows[0]["CongGiaoTiep"].ToString());

                        break;
                    case "PIN":

                        InsertRow(table, "Cell", dt.Rows[0]["Cell"].ToString());
                        InsertRow(table, "Cong Nghe", dt.Rows[0]["CongNghe"].ToString());

                        break;
                    case "HDD":

                        InsertRow(table, "Dung Luong", dt.Rows[0]["DungLuong"].ToString());
                        InsertRow(table, "Chuan", dt.Rows[0]["Chuan"].ToString());
                        InsertRow(table, "So Vong", dt.Rows[0]["SoVong"].ToString());

                        break;
                    case "MAINBOARD":

                        InsertRow(table, "CPU", dt.Rows[0]["Cpu"].ToString());
                        InsertRow(table, "RAM", dt.Rows[0]["Ram"].ToString());
                        InsertRow(table, "Chipset", dt.Rows[0]["Chipset"].ToString());
                        InsertRow(table, "Audio", dt.Rows[0]["Audio"].ToString());
                        InsertRow(table, "Video", dt.Rows[0]["Video"].ToString());
                        InsertRow(table, "Form-factor", dt.Rows[0]["form_factor"].ToString());
                        InsertRow(table, "IO-Control", dt.Rows[0]["io_control"].ToString());
                        InsertRow(table, "Network", dt.Rows[0]["Network"].ToString());
                        InsertRow(table, "Interface", dt.Rows[0]["Interface"].ToString());
                        InsertRow(table, "Expansion", dt.Rows[0]["Expansion"].ToString());

                        break;
                    case "MOUSE":

                        InsertRow(table, "Cong Giao Tiep", dt.Rows[0]["CongGiaoTiep"].ToString());
                        InsertRow(table, "Mau", dt.Rows[0]["Mau"].ToString());

                        break;
                    case "OS":

                        //InsertRow(table,"Cong Giao Tiep", dt.Rows[0]["CongGiaoTiep"].ToString());
                        //InsertRow(table, "Mau",dt.Rows[0]["Mau"].ToString());

                        break;
                    case "DESKTOP":
                        //sql = "SELECT DESKTOP.id, " +
                        //        "CPU.Socket, CPU.Bus, CPU.Cache, CPU.Speed, " +
                        //        "SOUND_CARD.CongSuat, SOUND_CARD.Ex, " +
                        //        "RAM.DungLuong, " +
                        //        "HDD.DungLuong AS Expr1, " +
                        //        "CD_DVD.TocDo, CD_DVD.CongGiaoTiep, " +
                        //        "VGA.Engine, VGA.VideoMemory, VGA.InterfaceMemory, VGA.CongGiaoTiep AS Expr9, " +
                        //        "KEYBOARD.CongGiaoTiep AS Expr11, KEYBOARD.Mau, " +
                        //        "MOUSE.CongGiaoTiep AS Expr13, MOUSE.Mau AS Expr14, " +
                        //        "MAINBOARD.Cpu, MAINBOARD.Ram, MAINBOARD.Chipset, MAINBOARD.Audio, MAINBOARD.Video, MAINBOARD.form_factor, MAINBOARD.io_control, MAINBOARD.Network, MAINBOARD.Interface, MAINBOARD.Expansion, MONITOR.DoPhanGiai, MONITOR.DoTuongPhan, MONITOR.ThoiGianDapUng, " +
                        //        "MONITOR.CongGiaoTiep AS Expr17, " +
                        //        "POWER.CongSuat AS Expr19, POWER.Quat, POWER.Connector, " +
                        //        "SPEAKER.CongSuat AS Expr21, SPEAKER.Chuan AS Expr22" +
                        InsertRow(table, "CPU", "Socket: " + dt.Rows[0]["Socket"].ToString(), "Bus : " + dt.Rows[0]["Bus"].ToString(), "Cache : " + dt.Rows[0]["Cache"].ToString(), "Tốc độ: " + dt.Rows[0]["Socket"].ToString());
                        InsertRow(table, "Card âm thanh", "Công suất: " + dt.Rows[0]["CongSuat"].ToString(), "Mở rộng: " + dt.Rows[0]["Ex"].ToString());
                        InsertRow(table, "RAM", "Dung lượng: " + dt.Rows[0]["DungLuong"].ToString());
                        InsertRow(table, "HDD", "Dung lượng: " + dt.Rows[0]["Expr1"].ToString());
                        InsertRow(table, "CD_DVD", "Tốc độ: " + dt.Rows[0]["TocDo"].ToString(), "Cổng giao tiếp : " + dt.Rows[0]["CongGiaoTiep"].ToString());
                        InsertRow(table, "VGA", "Engine: " + dt.Rows[0]["Engine"].ToString(), "Bộ nhớ video : " + dt.Rows[0]["VideoMemory"].ToString(), "InterfaceMemory : " + dt.Rows[0]["InterfaceMemory"].ToString(), "Cổng giao tiếp: " + dt.Rows[0]["Expr9"].ToString());
                        InsertRow(table, "Chuột", "Cổng giao tiếp: " + dt.Rows[0]["Expr13"].ToString(), "Màu sắc : " + dt.Rows[0]["Expr14"].ToString());
                        InsertRow(table, "Bàn phiếm", "Cổng giao tiếp: " + dt.Rows[0]["Expr11"].ToString(), "Màu sắc : " + dt.Rows[0]["Mau"].ToString());
                        //InsertRow(table, "Mainboard", "Cổng giao tiếp: " + dt.Rows[0]["KEYBOARD.CongGiaoTiep"].ToString(), "Màu sắc : " + dt.Rows[0]["KEYBOARD.Mau"].ToString());
                        InsertRow(table, "Màn hình", "Cổng giao tiếp: " + dt.Rows[0]["Expr17"].ToString());
                        InsertRow(table, "Nguồn", "Công suất: " + dt.Rows[0]["Expr19"].ToString(), "Quạt : " + dt.Rows[0]["Quat"].ToString());
                        InsertRow(table, "Loa", "Công suất: " + dt.Rows[0]["Expr21"].ToString(), "Chuẩn : " + dt.Rows[0]["Expr22"].ToString());

                        break;
                    case "LAPTOP":
                        InsertRow(table, "Mô tả Sơ lượt", "Kích thướt màn hình: " + dt.Rows[0]["ScreenSize"].ToString(), "Khối lượng : " + dt.Rows[0]["KhoiLuong"].ToString());
                        InsertRow(table, "CPU", "Bus : " + dt.Rows[0]["Bus"].ToString(), "Cache : " + dt.Rows[0]["Cache"].ToString(), "Tốc độ: " + dt.Rows[0]["Socket"].ToString());
                        InsertRow(table, "Card âm thanh", "Công suất: " + dt.Rows[0]["CongSuat"].ToString(), "Mở rộng: " + dt.Rows[0]["Ex"].ToString());
                        InsertRow(table, "RAM", "Dung lượng: " + dt.Rows[0]["DungLuong"].ToString());
                        InsertRow(table, "HDD", "Dung lượng: " + dt.Rows[0]["Expr4"].ToString());
                        InsertRow(table, "CD_DVD", "Tốc độ: " + dt.Rows[0]["TocDo"].ToString(), "Cổng giao tiếp : " + dt.Rows[0]["CongGiaoTiep"].ToString());
                        InsertRow(table, "VGA", "Engine: " + dt.Rows[0]["Engine"].ToString(), "Bộ nhớ video : " + dt.Rows[0]["VideoMemory"].ToString(), "InterfaceMemory : " + dt.Rows[0]["InterfaceMemory"].ToString(), "Cổng giao tiếp: " + dt.Rows[0]["Expr10"].ToString());
                        //InsertRow(table, "Mainboard", "Cổng giao tiếp: " + dt.Rows[0]["KEYBOARD.CongGiaoTiep"].ToString(), "Màu sắc : " + dt.Rows[0]["KEYBOARD.Mau"].ToString());
                        InsertRow(table, "Pin", "Cell: " + dt.Rows[0]["Cell"].ToString(), "Công nghệ : " + dt.Rows[0]["CongNghe"].ToString());

                        //sql = "SELECT LAPTOP.id, LAPTOP.ScreenSize, LAPTOP.KhoiLuong, " +
                        //    "CPU.Bus, CPU.Cache, CPU.Speed, " +
                        //    "SOUND_CARD.CongSuat, SOUND_CARD.Ex, " +
                        //    "RAM.Bus AS Expr4, RAM.DungLuong, " +
                        //    "HDD.DungLuong AS Expr6, " +
                        //    "CD_DVD.TocDo, CD_DVD.CongGiaoTiep, " +
                        //    "PIN.Cell, PIN.CongNghe, " +
                        //    "VGA.Engine, VGA.VideoMemory, VGA.InterfaceMemory, VGA.CongGiaoTiep AS Expr10, " +
                        //    "MAINBOARD.Cpu, MAINBOARD.Ram, MAINBOARD.Chipset, MAINBOARD.Audio, MAINBOARD.Video, MAINBOARD.form_factor, MAINBOARD.io_control, MAINBOARD.Network, MAINBOARD.Interface, MAINBOARD.Expansion, " +
                        //    "CPU.Socket " +
                        break;
                }

                table.DataBind();
            }
        }

        private void InsertRow(Table table, string title, params string[] lstContent)
        {
            //add row title
            TableRow row = new TableRow();
            TableCell cell = new TableCell();

            cell.CssClass = "Table_Header";
            cell.ColumnSpan = 3;

            cell.Font.Bold = true;// = font;

            cell.Text = title;
            cell.VerticalAlign = VerticalAlign.Middle;

            row.Controls.Add(cell);
            table.Controls.Add(row);

            row = new TableRow();
            cell = new TableCell();
            cell.Font.Bold = true;
            cell.VerticalAlign = VerticalAlign.Middle;
            //cell.CssClass = "table_data";

            cell.Text = "<ul>";
            int i;
            for (i = 0; i < lstContent.Length; i++)
            {
                cell.Text += "<li>" + lstContent[i] + "</li>";
            }
            cell.Text += "</ul";

            row.Controls.Add(cell);
            table.Controls.Add(row);
        }

    }
}
